function defineProperty(obj, key, value) {
    return key in obj ? Object.defineProperty(obj, key, {
        value: value,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : obj[key] = value, obj;
}

var app, dataApi, globalApp = getApp(), dataProvider = require("../../provider/dataApi.js"), wxParse = (require("../../provider/pipe.js"), 
require("../../wxParse/wxParse.js"));

Page((defineProperty(dataApi = {
    data: (app = {
        title: "全真模拟",
        paperId: "",
        startPoint: [ 0, 0 ],
        curPoint: [],
        statusBarHeight: "",
        titleBarHeight: "",
        show: !1,
        num: 0,
        numLength: 0,
        remainder: 0,
        handpaperShow: !1,
        item: {
            total_score: 100,
            pass_score: 60,
            paper_time: 90,
            highest: 0
        },
        timer: null,
        ruleTime: "",
        durationTime: "",
        useTime: "",
        index: 0,
        paperDetail: "",
        answerList: [],
        audioList: [],
        answerLength: 0,
        isAudio: 1,
        inputHeight: 0,
        examType: ""
    }, defineProperty(app, "paperId", ""), defineProperty(app, "isLoading", !1), defineProperty(app, "isPageWait", !0), defineProperty(app, "playTime", "00:00"), 
    defineProperty(app, "totalTime", "00:00"), defineProperty(app, "progressBar", 0), defineProperty(app, "audioTimeList", []), defineProperty(app, "audioTimer", null), 
    app),
    mytouchstart: function(event) {
        this.setData({
            startPoint: [ event.touches[0].pageX, event.touches[0].pageY ]
        });
    },
    mytouchmove: function(event) {
        this.setData({
            curPoint: [ event.touches[0].pageX, event.touches[0].pageY ]
        });
    },
    mytouchend: function(event) {
        event.changedTouches[0].pageX - this.data.startPoint[0] < 0 ? this.data.index + 1 < this.data.paperDetail.length && this.setData({
            index: this.data.index + 1
        }) : 0 < this.data.index && this.setData({
            index: this.data.index - 1
        });
    },
    bindplay: function(event) {
        if (console.log(event), event = event.currentTarget.id, this.data.playIndex) {
            var videoContext = wx.createVideoContext([ "index", this.data.playIndex ].join(""));
            videoContext.seek(0), videoContext.pause();
        }
        this.setData({
            playIndex: event
        }), wx.createVideoContext([ "index", this.data.playIndex ].join("")).play(), this.readVideo(event);
    },
    onLoad: function(options) {
        wx.getStorageSync("uid") ? (this.setData({
            statusBarHeight: globalApp.globalData.statusBarHeight,
            titleBarHeight: globalApp.globalData.titleBarHeight,
            item: globalApp.globalData.item,
            ruleTime: 60 * globalApp.globalData.item.paper_time
        }), options.examType && (this.setData({
            examType: options.examType
        }), "1" == options.examType && this.setData({
            paperId: options.paperId
        }), this.mockExam(options.examType), this.timer(60 * options.time)), "2" == options.examType && (this.setData({
            title: "未做试题"
        }), this.timer(60 * globalApp.globalData.item.paper_time)), "3" == options.examType && (this.setData({
            title: "智能考试"
        }), this.timer(60 * globalApp.globalData.item.paper_time))) : wx.navigateTo({
            url: "/pages/login/login"
        });
    },
    goBack: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    showTk: function() {
        if (100 < this.data.paperDetail) {
            var totalQuestions = Math.floor(this.data.paperDetail.length / 100), remainingQuestions = this.data.paperDetail.length % 100;
            console.log(totalQuestions, remainingQuestions), this.setData({
                num: 100,
                numLength: totalQuestions,
                remainder: remainingQuestions
            });
        } else this.setData({
            num: this.data.paperDetail
        });
        this.setData({
            show: !0
        });
    },
    onClose: function() {
        this.setData({
            show: !1
        });
    },
    goUp: function() {
        if (0 == this.data.index) return wx.showToast({
            title: "这是第一题",
            icon: "none"
        }), !1;
        this.setData({
            index: this.data.index - 1,
            isPageWait: !0
        }), this.changepic(this.data.index), this.wxParseTitle(this.data.index), this.clearAudioTime();
    },
    goDown: function() {
        if (this.data.index + 1 >= this.data.paperDetail.length) return wx.showToast({
            title: "已经是最后一题了",
            icon: "none"
        }), !1;
        this.setData({
            index: this.data.index + 1,
            isPageWait: !0
        }), this.changepic(this.data.index), this.wxParseTitle(this.data.index), this.clearAudioTime();
    },
    jumptap: function(event) {
        this.setData({
            index: event.currentTarget.dataset.i,
            show: !1,
            isPageWait: !0
        }), this.changepic(this.data.index), this.wxParseTitle(this.data.index), this.clearAudioTime();
    },
    scrolltolower: function(event) {
        this.data.num < this.data.paperDetail.length && (Math.floor(this.data.num / 100) == this.data.numLength ? this.setData({
            num: this.data.num + this.data.remainder
        }) : this.setData({
            num: this.data.num + 100
        }));
    },
    timer: function(duration) {
        clearInterval(this.data.timer);
        var self = this, remainingTime = duration;
        self.setData({
            durationTime: self.djstimer(remainingTime)
        }), this.data.timer = setInterval(function() {
            remainingTime--, self.setData({
                durationTime: self.djstimer(remainingTime),
                useTime: self.djstimer(duration - remainingTime)
            }), remainingTime <= 0 && (clearInterval(self.data.timer), self.mockexam_submit());
        }, 1e3);
    },
    djstimer: function(time) {
        var hours = (hours = Math.floor(time / 60 / 60 % 24)) < 10 ? "0" + hours : hours, minutes = (minutes = Math.floor(time / 60 % 60)) < 10 ? "0" + minutes : minutes;
        return time = (time = Math.floor(time % 60)) < 10 ? "0" + time : time, 0 < hours ? hours + ":" + minutes + ":" + time : minutes + ":" + time;
    },
    goHandpaper: function() {
        this.setData({
            handpaperShow: !0
        });
    },
    Handpaper: function() {
        this.setData({
            handpaperShow: !1
        }), this.mockexam_submit();
    },
    goDati: function() {
        this.setData({
            handpaperShow: !1
        });
    },
    honClose: function() {
        this.setData({
            handpaperShow: !1
        });
    },
    choice: function(event) {
        var answerList = this.data.answerList;
        answerList[event.currentTarget.dataset.fjtit].choose = event.currentTarget.dataset.option, this.setData({
            answerList: answerList
        }), this.getAnswerlength();
    },
    multipleChoice: function(event) {
        var questionIndex = event.currentTarget.dataset.fjtit, optionIndex = event.currentTarget.dataset.option, isChosen = 0;
        (event = this.data.answerList)[questionIndex].choose.forEach(function(option, index) {
            option.o == optionIndex && (option.ischoose = !option.ischoose);
        });
        for (var i = 0; i < event[questionIndex].choose.length; i++) {
            if (event[questionIndex].choose[i].ischoose) {
                isChosen = 1, event[questionIndex].flag = !1;
                break;
            }
            isChosen = 0, event[questionIndex].flag = !1;
        }
        event[questionIndex].flag = 1 == isChosen, this.setData({
            answerList: event
        }), this.getAnswerlength();
    },
    textInput: function(event) {
        var questionIndex = event.currentTarget.dataset.fjnum, answerList = this.data.answerList;
        answerList[questionIndex].choose[event.currentTarget.dataset.inputnum] = event.detail.value, this.isempty(answerList[questionIndex].choose) ? answerList[questionIndex].issure = !0 : answerList[questionIndex].issure = !1, 
        this.setData({
            answerList: answerList
        }), this.getAnswerlength();
    },
    vioceChoice: function(event) {
        console.log(event);
        var questionIndex = event.currentTarget.dataset.fjnum, answerList = this.data.answerList, data = {};
        for (this.setData((data["answerList[" + questionIndex + "].choose[" + event.currentTarget.dataset.zjnum + "].choose"] = event.currentTarget.dataset.option, 
        data)), event = !0, data = 0; data < answerList[questionIndex].choose.length; data++) if ("" == answerList[questionIndex].choose[data].choose) {
            event = !1;
            break;
        }
        answerList[questionIndex].iscomplete = event, this.setData({
            answerList: answerList
        }), this.getAnswerlength();
    },
    chooseOption: function(event) {
        var questionIndex = this.data.index, answerList = this.data.answerList;
        answerList[questionIndex].choose[event.currentTarget.dataset.findex].choose = event.currentTarget.dataset.zindex, 
        event = !0;
        for (var i = 0; i < answerList[questionIndex].choose.length; i++) if ("" == answerList[questionIndex].choose[i].choose) {
            event = !1;
            break;
        }
        answerList[questionIndex].issure = event, this.setData({
            answerList: answerList
        }), this.getAnswerlength();
    },
    bindTextAreaBlur: function(event) {
        var questionIndex = event.currentTarget.dataset.index;
        event = event.detail.value;
        var answerList = this.data.answerList;
        answerList[questionIndex].choose = event, answerList[questionIndex].issure = "" != event, this.setData({
            answerList: answerList
        }), this.getAnswerlength();
    },
    getAnswerlength: function() {
        var answeredCount = 0;
        this.data.answerList.forEach(function(question) {
            1 == question.type || 3 == question.type || 8 == question.type ? question.choose && answeredCount++ : 2 == question.type ? question.flag && answeredCount++ : 4 == question.type ? question.issure && answeredCount++ : 5 == question.type ? question.iscomplete && answeredCount++ : (6 == question.type || 7 == question.type) && question.issure && answeredCount++;
        }), this.setData({
            answerLength: answeredCount
        });
    },
    onReady: function() {
        var audioContext = wx.createInnerAudioContext();
        this.setData({
            audioCtx: audioContext
        });
    },
    onHide: function() {
        this.changepic(this.data.index), wx.createVideoContext([ "index", this.data.playIndex ].join("")).pause();
    },
    onUnload: function() {
        this.changepic(this.data.index), wx.createVideoContext([ "index", this.data.playIndex ].join("")).pause();
        var timer = clearInterval(this.data.audioTimer);
        this.setData({
            audioTimer: timer
        });
    },
    mockExam: function(examType) {
        var self = this;
        this.setData({
            isLoading: !0
        });
        var paperId = "";
        "1" == examType && (paperId = this.data.paperId), dataProvider.mockExam({
            uid: wx.getStorageSync("uid"),
            exam_type: examType,
            paperid: paperId
        }).then(function(response) {
            if (console.log(response), response.data.list.length <= 0) self.setData({
                isLoading: !1
            }); else {
                console.log(response);
                var answerList = [], audioList = [], tempList = [];
                response.data.list && (response.data.list.forEach(function(question) {
                    if (1 == question.type || 3 == question.type || 8 == question.type) answerList.push({
                        type: question.type,
                        tid: question.id,
                        issure: !1,
                        choose: ""
                    }); else if (2 == question.type) {
                        var options = [];
                        question.option.forEach(function(option) {
                            options.push({
                                o: option.o,
                                ischoose: !1
                            });
                        }), answerList.push({
                            type: question.type,
                            tid: question.id,
                            issure: !1,
                            flag: !1,
                            choose: options
                        });
                    } else if (4 == question.type) {
                        for (var answers = [], i = 0; i < question.rightarray.length; i++) answers.push("");
                        answerList.push({
                            type: question.type,
                            tid: question.id,
                            issure: !1,
                            choose: answers
                        });
                    } else if (5 == question.type) {
                        var subQuestions = [];
                        question.list.forEach(function(subQuestion) {
                            subQuestions.push({
                                tid: subQuestion.id,
                                choose: ""
                            });
                        }), answerList.push({
                            type: question.type,
                            tid: question.id,
                            iscomplete: !1,
                            choose: subQuestions
                        });
                    } else (6 == question.type || 7 == question.type) && question.list instanceof Array && 0 < question.list.length && (answers = question.list.map(function(subQuestion, index) {
                        return {
                            tid: subQuestion.id,
                            choose: ""
                        };
                    }), answerList.push({
                        type: question.type,
                        tid: question.id,
                        issure: !1,
                        choose: answers
                    }));
                    question.qaudio ? audioList.push({
                        audiosrc: question.qaudio,
                        time: 0
                    }) : audioList.push({
                        audiosrc: "",
                        time: 0
                    }), 2 == question.a_type ? tempList.push(question.option) : tempList.push("");
                }), console.log(answerList), response.data.list && self.setData({
                    paperDetail: response.data.list,
                    answerList: answerList,
                    audioList: audioList
                }), response.data.paperid && self.setData({
                    paperId: response.data.paperid
                }), self.setData({
                    isLoading: !1
                }), self.getAllAudioTime(), self.wxParseTitle(self.data.index));
            }
        }).catch(function(error) {
            self.setData({
                isLoading: !1
            }), console.log(error);
        });
    },
    wxParseTitle: function(questionIndex) {
        var question = this.data.paperDetail[questionIndex];
        wxParse.wxParse("ParseTitle", "html", question.question, this, 5), 
        0 != question.a_type || 1 != question.type && 2 != question.type ? this.setData({
            isPageWait: !1
        }) : this.wxParseOptions(question.option);
    },
    wxParseOptions: function(options) {
        console.log(options);
        for (var i = 0; i < options.length; i++) wxParse.wxParse("wxParseOption" + i, "html", options[i].p, this);
        console.log(this.data.wxParseOption0), this.setData({
            isPageWait: !1
        });
    },
    isempty: function(array) {
        var isEmpty;
        for (var key in new Boolean(), isEmpty = !0, array) array[key] || (isEmpty = !1);
        return !!isEmpty;
    },
    changepic: function(questionIndex) {
        this.data.paperDetail.length <= 0 || 0 == this.data.audioList.length || this.data.audioList[questionIndex].url && !(this.data.audioList[questionIndex].url != this.data.audioList[questionIndex - 1].url && 0 < questionIndex) || (this.setData({
            isAudio: 1
        }), this.data.audioCtx.pause());
    },
    getAllAudioTime: function() {
        var self = this, audioTimes = [];
        this.data.paperDetail.forEach(function(question, index) {
            if (question.qaudio) {
                var audioContext = self.data.audioCtx;
                audioContext.src = question.qaudio, console.log(audioContext), setTimeout(function() {
                    audioContext.currentTime;
                    var duration = Math.round(audioContext.duration);
                    audioTimes.push(self.handleTime(duration));
                }, 2 * index);
            } else audioTimes.push("00:00");
        }), console.log(audioTimes);
    },
    myAudioStart: function(event) {
        var audioContext = this.data.audioCtx, self = this, audioList = this.data.audioList;
        this.setData({
            isAudio: 2
        }), audioContext.src = event.currentTarget.dataset.src, 0 != audioList[event.currentTarget.dataset.num].time ? (audioContext.play(), 
        audioContext.seek(audioList[event.currentTarget.dataset.num].time)) : audioContext.play(), this.currenttime(), this.data.onEnded || (this.data.onEnded = !0, 
        audioContext.onEnded(function() {
            self.setData({
                isAudio: 1,
                currentTime: 0
            }), self.audiotime(event.currentTarget.dataset.src, 0);
        }));
        var audioIndex = event.currentTarget.dataset.num, audioTimeList = self.data.audioTimeList, audioTimer = setInterval(function() {
            var currentTime, duration = Math.round(audioContext.currentTime), totalDuration = Math.round(audioContext.duration);
            currentTime = 100 * (duration / totalDuration).toFixed(3);
            var currentTimeFormatted = self.handleTime(duration), totalDurationFormatted = self.handleTime(totalDuration);
            audioTimeList[audioIndex] = totalDurationFormatted, self.setData({
                playTime: currentTimeFormatted,
                totalTime: totalDurationFormatted,
                progressBar: currentTime,
                audioTimeList: audioTimeList,
                audioTimer: audioTimer
            }), totalDuration == duration && (console.log("停止"), setTimeout(function() {
                clearInterval(audioTimer), self.setData({
                    playTime: "00:00",
                    totalTime: totalDurationFormatted,
                    progressBar: 0,
                    audioTimer: audioTimer
                });
            }, 1e3));
        }, 1e3);
    },
    myAudioPause: function(event) {
        var audioContext = this.data.audioCtx, self = this;
        audioContext.pause(), this.data.onPause || (this.data.onPause = !0, audioContext.onPause(function() {
            audioContext.currentTime, self.setData({
                isAudio: 1
            }), self.audiotime(event.currentTarget.dataset.src, Math.floor(audioContext.currentTime));
        }));
        var audioTimer = clearInterval(self.data.audioTimer);
        self.setData({
            audioTimer: audioTimer
        });
    },
    handleTime: function(seconds) {
        if (60 <= seconds) {
            var minutes = parseInt(seconds / 60 % 60);
            return (minutes < 10 ? "0" + minutes : minutes) + ":" + ((seconds = parseInt(seconds % 60)) < 10 ? "0" + seconds : seconds);
        }
        return "00:" + (seconds < 10 ? "0" + seconds : seconds);
    },
    clearAudioTime: function() {
        var questionIndex = this.data.index, audioTimeList = this.data.audioTimeList;
        0 < audioTimeList.length && (questionIndex = audioTimeList[questionIndex], clearInterval(this.data.audioTimer), console.log(audioTimeList), 
        this.setData({
            playTime: "00:00",
            totalTime: questionIndex,
            progressBar: 0
        }));
    },
    currenttime: function() {
        var audioContext = this.data.audioCtx;
        setTimeout(function() {
            audioContext.currentTime, audioContext.onTimeUpdate(function() {});
        }, 100);
    },
    audiotime: function(src, time) {
        this.data.audioList.forEach(function(audio) {
            audio.url == src && (audio.time = time);
        });
    },
    mockexam_submit: function() {
        var self = this, answers = [];
        this.data.answerList.forEach(function(question) {
            if (1 == question.type || 3 == question.type || 8 == question.type) answers.push({
                tid: question.tid,
                type: question.type,
                answer: question.choose
            }); else if (2 == question.type) {
                var selectedOptions = [];
                question.choose.forEach(function(option) {
                    option.ischoose && selectedOptions.push(option.o);
                }), answers.push({
                    tid: question.tid,
                    type: question.type,
                    answer: selectedOptions
                });
            } else if (4 == question.type) {
                var formattedAnswers = [];
                question.choose.forEach(function(answer) {
                    formattedAnswers.push("【" + answer + "】");
                }), formattedAnswers = formattedAnswers.join(","), answers.push({
                    tid: question.tid,
                    type: question.type,
                    answer: formattedAnswers
                });
            } else {
                var subAnswers = [];
                question.choose.forEach(function(subAnswer) {
                    subAnswers.push({
                        tid: subAnswer.tid,
                        answer: subAnswer.choose
                    });
                }), answers.push({
                    tid: question.tid,
                    type: question.type,
                    answer: subAnswers
                });
            }
        }), dataProvider.mockexam_submit({
            uid: wx.getStorageSync("uid"),
            exam_type: this.data.examType,
            paperid: this.data.paperId,
            answerdata: JSON.stringify(answers),
            usetime: this.data.useTime
        }).then(function(response) {
            console.log(response), response.data.resuser && (globalApp.globalData.resuser = response.data.resuser, wx.redirectTo({
                url: "../result/result?type=1&paperid=" + self.data.paperId + "&id=" + response.data.rid
            }));
        }).catch(function(error) {
            console.log(error);
        });
    }
}, "onUnload", function() {
    clearInterval(this.data.timer);
}), defineProperty(dataApi, "onShareAppMessage", function() {
    return {
        path: "/pages/home/home"
    };
}), dataApi));